//
//  NoticeVC.swift
//  StoreProject
//
//  Created by CYC on 2021/9/30.
//

import UIKit

class NoticeVC: BaseViewController, UITableViewDelegate, UITableViewDataSource {
    
    var model = DayModel()
    var dataList = [DateNoticeModel]()
    var tableView = UITableView()
    
    // MARK:======================================生命周期========================================
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titleLabel?.text = "事件列表"

        self.creatUIAction()
        self.loadDataAction()
    }
    
    func creatUIAction() {
        
        if true {
            
            tableView = UITableView.init(frame: CGRect.init(x: 0, y: kNavHeight, width: kScreenWidth, height: kScreenHeight - kNavHeight), style: .plain)
            tableView.backgroundColor = .clear
            tableView.separatorStyle = .none
            tableView.rowHeight = 170
            tableView.estimatedRowHeight = 0
            tableView.estimatedSectionHeaderHeight = 0
            tableView.estimatedSectionFooterHeight = 0
            tableView.tableFooterView = UIView.init(frame: CGRect.zero)
            tableView.register(NoticeVCCell.classForCoder(), forCellReuseIdentifier: "NoticeVCCell")
            tableView.delegate = self
            tableView.dataSource = self
            tableView.tableFooterView = UIView.init(frame: .init(x: 0, y: 0, width: kScreenWidth, height: kSpaceSize))
            self.view.addSubview(tableView)
        }
        
    
        
    }
    
    
    // MARK:======================================点击响应========================================
    
    
    
    // MARK:======================================网络请求========================================
    
    // MARK:获取数据
    @objc func loadDataAction() {
        
        self.dataList.removeAll()
        for item in DateNoticeModel.getDatas() {
            
            if item.year.integerValue == self.model.date.year &&
                item.month.integerValue == self.model.date.month &&
                item.day.integerValue == self.model.date.day
                {
                
                self.dataList.append(item)
            }
        }
        
        self.tableView.reloadData()
        
        if self.dataList.count == 0 {
            self.tableView.addSubview(self.noDataLabel!)
        } else {
            self.noDataLabel?.removeFromSuperview()
        }
    }
    
    // MARK:======================================代理方法========================================
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if self.dataList.count > indexPath.row {
            
            let model = self.dataList[indexPath.row]
            
            let text = model.text
            let size = text.boundingRect(with: .init(width: kScreenWidth - kSpaceSize*2 - kContentSize*2, height: CGFloat(MAXFLOAT)),
                                         options: .usesLineFragmentOrigin,
                                         attributes: [NSAttributedString.Key.font : kMFont(16)],
                                         context: nil);
            
            return kSpaceSize + size.height + kContentSize*2
        }
        
        return 0.0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "NoticeVCCell", for: indexPath) as! NoticeVCCell
        
        if self.dataList.count > indexPath.row {
            
            let model = self.dataList[indexPath.row]
            cell.creatUIAction(model: model)
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    // MARK:左滑删除
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        
        if indexPath.row < self.dataList.count {
            
            let model = self.dataList[indexPath.row]
            
            DateNoticeModel.deleteData(model.noticeId)
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNoticeChange), object: nil)   // 发送通知
            
            self.dataList.remove(at: indexPath.row)
            self.tableView.reloadData()
            
            if self.dataList.count == 0 {
                self.tableView.addSubview(self.noDataLabel!)
            }
            
        }
    }
    
}

class NoticeVCCell: UITableViewCell {
    
    var model = DateNoticeModel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("")
    }
    
    // MARK:创建UI
    func creatUIAction(model: DateNoticeModel) {
        
        self.model = model
        self.contentView.removeAllSubviews()
        
        let text = self.model.text
        let size = text.boundingRect(with: .init(width: kScreenWidth - kSpaceSize*2 - kContentSize*2, height: CGFloat(MAXFLOAT)),
                                     options: .usesLineFragmentOrigin,
                                     attributes: [NSAttributedString.Key.font : kMFont(16)],
                                     context: nil);
        
        let view1 = UIView.init(frame: CGRect.init(x: kSpaceSize, y: kSpaceSize / 2.0, width: kScreenWidth - kSpaceSize*2, height: size.height + kContentSize*2))
        view1.backgroundColor = .white
        view1.c_radius(radius: kRadiusSizeL)
        self.contentView.addSubview(view1)
        
        
        let lab = UILabel.init(frame: CGRect.init(x: kContentSize, y: kContentSize, width: view1.c_width() - kContentSize*2, height: size.height))
        lab.textColor = kLabelColor1
        lab.textAlignment = .left
        lab.font = kMFont(16)
        lab.numberOfLines = 0
        lab.text = text
        view1.addSubview(lab)
        
        
    }
    
}

